<?php

class Renew_Model_BukkenCampaign extends Apm_Model_Base
{

    public function getHeyaCampaigns($code)
    {
        $sql = "
			select
				b.M94_CAMPAIGN_CD,
				b.M94_CAMPAIGN_URL,
				b.M94_CAMPAIGN_NAME,
                c.M94_ITEM_ICON,
				c.M94_ITEM_BANNER,
				'' AS M94_ITEM_NAME,
				d.T03_PONTA_TINRYO_FLG,
				d.T03_PONTA_TINRYO_POINT,
				d.T03_PONTA_SEIYAKU_FLG,
				d.T03_PONTA_SEIYAKU_POINT
			from
			        T03WEB_HEYA_CAMPAIGN a
			        ,M94CAMPAIGN         b
			        ,M94CAMPAIGN_ITEM    c
					,T03WEB_HEYA		 d
			WHERE d.T03_HEYA_CD = " . $code . "
			AND b.M94_CAMPAIGN_CD = a.T03_CAMPAIGN_CD
			AND NVL(b.M94_START_YMD,'99999999') <= TO_CHAR(SYSDATE, 'YYYYMMDD')
			AND NVL(b.M94_END_YMD,'99999999')   >= TO_CHAR(SYSDATE, 'YYYYMMDD')
			AND b.M94_DELETE_FLAG = 0
			AND b.M94_MOBILE_KBN  = '0'
			AND c.M94_CAMPAIGN_CD = a.T03_CAMPAIGN_CD
			AND (c.M94_ITEM_CD     = a.T03_OTAKARA_CD or c.M94_CAMPAIGN_CD = 11)
			AND NVL(c.M94_INSERT_DATE, SYSDATE+1) <= SYSDATE
			AND c.M94_DELETE_FLAG = 0
			AND (c.M94_DELETE_DATE > SYSDATE or c.M94_DELETE_DATE is null)
			AND a.T03_HEYA_CD = d.T03_HEYA_CD
			order by b.M94_CAMPAIGN_TOKUSYU_KBN, b.M94_CAMPAIGN_PRIORITY_NO, b.M94_START_YMD desc";

        $data = $this->dbGetList($sql);
        // return $data;
        // キャンペーンCDで関連づけた連想配列に変換
        $campaignList = array();
        foreach ($data as $campaign)
        {
            if ($campaign['M94_CAMPAIGN_CD'] == Apm_Config_Search::PONTA_CAMPAIGN_CD)
            {
                // ポンタ部屋の場合
                if ($campaign['T03_PONTA_TINRYO_FLG'] == '1' && $campaign['T03_PONTA_SEIYAKU_FLG'] == '1')
                {
                    // 月額家賃ポイント＆特別成約ポイント
                    $campaign['M94_ITEM_NAME'] = '成約時に仲介手数料１％＋' . $campaign['T03_PONTA_SEIYAKU_POINT'] . 'pt&nbsp;＆毎月' . $campaign['T03_PONTA_TINRYO_POINT'] . 'pt貯まる！';
                }
                else if ($campaign['T03_PONTA_TINRYO_FLG'] == '1')
                {
                    // 月額家賃ポイント
                    $campaign['M94_ITEM_NAME'] = '毎月' . $campaign['T03_PONTA_TINRYO_POINT'] . 'pt貯まる！';
                }
                else if ($campaign['T03_PONTA_SEIYAKU_FLG'] == '1')
                {
                    // 特別成約ポイント
                    $campaign['M94_ITEM_NAME'] = '成約時に仲介手数料１％＋' . $campaign['T03_PONTA_SEIYAKU_POINT'] . 'pt貯まる！';
                }
            }
            $campaignList[] = $campaign;
        }

        return $campaignList;
    }

    public function getNormalCampaigns()
    {
        $sql = "
			SELECT
				b.M94_CAMPAIGN_CD,
				b.M94_CAMPAIGN_URL,
				b.M94_CAMPAIGN_NAME,
                c.M94_ITEM_ICON,
				c.M94_ITEM_BANNER,
				'' AS M94_ITEM_NAME,
				NULL AS T03_PONTA_TINRYO_FLG,
				NULL AS T03_PONTA_TINRYO_POINT,
				NULL AS T03_PONTA_SEIYAKU_FLG,
				NULL AS T03_PONTA_SEIYAKU_POINT
			FROM
				M94CAMPAIGN b
					LEFT OUTER JOIN M94CAMPAIGN_ITEM c
						ON c.M94_CAMPAIGN_CD = b.M94_CAMPAIGN_CD
						AND c.M94_ITEM_CD = 1
						AND NVL(c.M94_INSERT_DATE, SYSDATE+1) <= SYSDATE
						AND c.M94_DELETE_FLAG = 0
						AND (c.M94_DELETE_DATE > SYSDATE or c.M94_DELETE_DATE is null)
			WHERE
				b.M94_CAMPAIGN_HYOUZI = 1
			AND b.M94_DELETE_FLAG = 0
			AND b.M94_CAMPAIGN_KBN IN (2,4)
			AND b.M94_MOBILE_KBN = 0
			AND NVL(b.M94_START_YMD,'99999999') <= TO_CHAR(SYSDATE, 'YYYYMMDD')
			AND NVL(b.M94_END_YMD,'99999999')   >= TO_CHAR(SYSDATE, 'YYYYMMDD')
			order by b.M94_CAMPAIGN_TOKUSYU_KBN, b.M94_CAMPAIGN_PRIORITY_NO, NVL(b.M94_START_YMD,'99999999') desc";
        return $this->dbGetList($sql);
    }

}
